//
//  LoopsDetailViewController.m
//  LuckyRadio
//
//  Created by JiaRui on 15/11/17.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "LoopsDetailViewController.h"
#import "CustomerNetworking.h"
@interface LoopsDetailViewController ()<UIWebViewDelegate>
@property (nonatomic, retain) NSMutableArray * loopsDetailArr;

@property (nonatomic, retain) UIWebView * webView;
@end

@implementation LoopsDetailViewController
-(void)dealloc
{
    [_webView release];
    [_loopsDetailArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"文章详情";
    self.loopsDetailArr = [NSMutableArray array];
    [self loadDetail];
    self.webView = [[UIWebView alloc]initWithFrame:self.view.frame];
    self.webView.delegate = self;
    self.webView.scalesPageToFit = YES;
    self.webView.scrollView.bounces = NO;
    [self.view addSubview:self.webView];
    [_webView release];
}
- (void)loadDetail
{
    NSString * url = self.url;
    
    NSArray * arr = [url componentsSeparatedByString:@"/"];
    NSString * urlStr = [arr lastObject];
    NSString * str = [NSString stringWithFormat:@"%@", urlStr];
    NSDictionary *body = @{@"client":@"1", @"contentid":str,@"deviceid":@"7F95D47F-BF62-4528-B18E-EE921FFC5323", @"version":@"3.0.6"};
    NSLog(@"%@", body);
    [CustomerNetworking customerWithURL:@"http://api2.pianke.me/article/info" andParamer:body andBlock:^(id response) {
        self.html = [[response objectForKey:@"data"]objectForKey:@"html"];
        [self.webView loadHTMLString:self.html baseURL:nil];
    }];

}

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    [webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth= 320;"
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     "oldwidth = myimg.width;"
     "myimg.width = maxwidth;"
     "myimg.height = myimg.height;"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
